﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using VioletDevelopmentLaboratory.Net.Data;
using VioletDevelopmentLaboratory.Net.Web.Configuration;

namespace VioletDevelopmentLaboratory.Net.Web.Component
{
	public enum SysopCreateResult { Success, Exists, Failure }
	public enum SysopDeleteResult { Success, UnknownSysop, Failure }

	public class Sysop
	{
		private String applicationName;
		public String ApplicationName { get { return this.applicationName; } }

		private String componentID;
		public String ComponentID { get { return this.componentID; } }

		private String userName;
		public String UserName { get { return this.userName; } }

		private String providerUserKey;
		public String ProviderUserKey { get { return this.providerUserKey; } }

		public static SysopCreateResult Add(String componentID, String providerUserKey, String username)
		{
			if (componentID == null)
				throw new ArgumentNullException("componentID");

			if (providerUserKey == null)
				throw new ArgumentNullException("providerUserKey");

			if (username == null)
				throw new ArgumentNullException("username");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentOutOfRangeException("componentID");

			if (String.IsNullOrEmpty(providerUserKey))
				throw new ArgumentOutOfRangeException("providerUserKey");

			if (String.IsNullOrEmpty(username))
				throw new ArgumentOutOfRangeException("username");

			Object returnValue = null;

			SqlClientAgent.ExecuteNonQuery(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_Sysop_Create",
				out returnValue,
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", componentID),
				new SqlParameter("@ProviderUserKey", providerUserKey),
				new SqlParameter("@UserName", username)
			);

			switch ((Int32)returnValue)
			{
				case 0: return SysopCreateResult.Success;
				case 1: return SysopCreateResult.Exists;
				default: return SysopCreateResult.Failure;
			}
		}

		public static SysopDeleteResult Remove(String componentID, String providerUserKey)
		{
			if (componentID == null)
				throw new ArgumentNullException("componentID");

			if (providerUserKey == null)
				throw new ArgumentNullException("providerUserKey");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentOutOfRangeException("componentID");

			if (String.IsNullOrEmpty(providerUserKey))
				throw new ArgumentOutOfRangeException("providerUserKey");

			Object returnValue = null;

			SqlClientAgent.ExecuteNonQuery(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_Sysop_Delete",
				out returnValue,
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", componentID),
				new SqlParameter("@ProviderUserKey", providerUserKey)
			);

			switch ((Int32)returnValue)
			{
				case 0: return SysopDeleteResult.Success;
				case 1: return SysopDeleteResult.UnknownSysop;
				default: return SysopDeleteResult.Failure;
			}
		}

		internal static Sysop Fill(SqlDataReader reader)
		{
			Sysop sysop = new Sysop();
			sysop.applicationName = reader["ApplicationName"].ToString();
			sysop.componentID = reader["ComponentID"].ToString();
			sysop.providerUserKey = reader["ProviderUserKey"].ToString();
			sysop.userName = reader["UserName"].ToString();
			return sysop;
		}

		public static Sysop GetSysop(String componentID, String providerUserKey)
		{
			if (componentID == null)
				throw new ArgumentNullException("componentID");

			if (providerUserKey == null)
				throw new ArgumentNullException("providerUserKey");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentOutOfRangeException("componentID");

			if (String.IsNullOrEmpty(providerUserKey))
				throw new ArgumentOutOfRangeException("providerUserKey");

			SqlDataReader reader = SqlClientAgent.ExecuteReader(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_Sysop_GetSysop",
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", componentID),
				new SqlParameter("@ProviderUserKey", providerUserKey)
			);

			Sysop sysop = null;

			try
			{
				if (reader.HasRows)
				{
					reader.Read();
					sysop = Fill(reader);
				}
			}
			finally
			{
				reader.Close();
			}
			
			return sysop;
		}

		public static SysopCollection GetSysopCollection(String componentID)
		{
			if (componentID == null)
				throw new ArgumentNullException("componentID");

			if (String.IsNullOrEmpty(componentID))
				throw new ArgumentException("Emtpy.", "componentID");

			SqlDataReader reader = SqlClientAgent.ExecuteReader(
				ConfigurationManager.ConnectionStrings[VWebConfigurationReader.ConnectionStringName].ConnectionString,
				CommandType.StoredProcedure,
				"VWeb_Sysop_GetSysops",
				new SqlParameter("@ApplicationName", VWebConfigurationReader.ApplicationName),
				new SqlParameter("@ComponentID", componentID)
			);

			SysopCollection sysops = null;

			try
			{
				if (reader.HasRows)
				{
					sysops = new SysopCollection();

					while (reader.Read())
					{
						sysops.Add(Fill(reader));
					}
				}
			}
			finally
			{
				reader.Close();
			}

			return sysops;
		}
	}

	public class SysopCollection : Collection<Sysop> { }
}
